package com.cskaoyan.market.service;


import com.cskaoyan.market.db.domain.MarketSystem;
import com.cskaoyan.market.db.domain.MarketSystemExample;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class MarketConfigServiceImpl implements MarketConfigService {
    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public Map<String, String> express() {
        Map<String, String> result = new HashMap<>();
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<String> list = new ArrayList<>();
        list.add("market_express_freight_value");
        list.add("market_express_freight_min");
        criteria.andKeyNameIn(list);
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        for (MarketSystem marketSystem : marketSystems) {
            result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        }
        return result;
    }

    @Override
    public Map<String, String> order() {
        Map<String, String> result = new HashMap<>();
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<String> list = new ArrayList<>();
        list.add("market_order_comment");
        list.add("market_order_unconfirm");
        list.add("market_order_unpaid");
        criteria.andKeyNameIn(list);
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        for (MarketSystem marketSystem : marketSystems) {
            result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        }
        return result;
    }

    @Override
    public Map<String, String> wx() {
        Map<String, String> result = new HashMap<>();
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<String> list = new ArrayList<>();
        list.add("market_wx_catlog_goods");
        list.add("market_wx_catlog_list");
        list.add("market_wx_index_brand");
        list.add("market_wx_index_hot");
        list.add("market_wx_index_new");
        list.add("market_wx_index_topic");
        list.add("market_wx_share");
        criteria.andKeyNameIn(list);
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        for (MarketSystem marketSystem : marketSystems) {
            result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        }
        return result;
    }

    @Override
    public Map<String, String> mall() {
        Map<String, String> result = new HashMap<>();
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
        List<String> list = new ArrayList<>();
        list.add("market_mall_address");
        list.add("market_mall_latitude");
        list.add("market_mall_longitude");
        list.add("market_mall_name");
        list.add("market_mall_phone");
        list.add("market_mall_qq");
        criteria.andKeyNameIn(list);
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExampleSelective(marketSystemExample, MarketSystem.Column.keyName, MarketSystem.Column.keyValue);
        for (MarketSystem marketSystem : marketSystems) {
            result.put(marketSystem.getKeyName(), marketSystem.getKeyValue());
        }
        return result;
    }

    @Override
    @Transactional
    public void update(Map<String, Object> map) {
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            MarketSystemExample marketSystemExample = new MarketSystemExample();
            MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
            criteria.andKeyNameEqualTo(entry.getKey());
            MarketSystem marketSystem = marketSystemMapper.selectOneByExampleSelective(marketSystemExample);
            marketSystem.setKeyValue(entry.getValue().toString());
            marketSystem.setUpdateTime(LocalDateTime.now());
            marketSystemMapper.updateByExampleSelective(marketSystem, marketSystemExample);
        }
    }
}

